package com.dims_hsrc.web.controller.data;

import com.dims_hsrc.common.annotation.Log;
import com.dims_hsrc.common.core.controller.BaseController;
import com.dims_hsrc.common.core.domain.AjaxResult;
import com.dims_hsrc.common.enums.BusinessType;
import com.dims_hsrc.data.domain.DataKnowledgeMapping;
import com.dims_hsrc.data.domain.DataKnowledgeMappingResult;
import com.dims_hsrc.data.service.IDataKnowledgeMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 知识映射 Controller
 *
 * @author 赵浩然
 */
@RestController
@RequestMapping("/data/knowledge_mapping")
public class DataKnowledgeMappingController extends BaseController {
    @Autowired
    private IDataKnowledgeMappingService service;

    /**
     * 查询知识映射列表
     */
    @GetMapping("/list")
    public AjaxResult list(DataKnowledgeMapping mapping) {
        System.out.println(new Throwable().getStackTrace()[0]);
        List<DataKnowledgeMapping> list = service.list(mapping);
        return success(list);
    }

    /**
     * 获取知识映射详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return success(service.getOne(id));
    }

    /**
     * 新增知识映射
     */
    @Log(title = "知识映射", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DataKnowledgeMapping mapping) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return toAjax(service.add(mapping));
    }

    /**
     * 修改知识映射
     */
    @Log(title = "知识映射", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@RequestBody DataKnowledgeMapping mapping) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return toAjax(service.update(mapping));
    }

    /**
     * 删除知识映射
     */
    @Log(title = "知识映射", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return toAjax(service.remove(ids));
    }

    /**
     * 根据结构ID获取知识映射
     */
    @GetMapping("/struct/{sid}")
    public AjaxResult getKnowledgeMappingsByStructId(@PathVariable("sid") String sid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return success(service.getKnowledgeMappingsByStructId(sid));
    }

    /**
     * 根据结构ID获取可用于映射的需求选项
     */
    @GetMapping("/demand_options")
    public AjaxResult getDemandOptions(@RequestParam String sid, @RequestParam String did) {
        System.out.println(new Throwable().getStackTrace()[0]);
        Map<String, Object> params = new HashMap<>();
        params.put("sid", sid);
        params.put("did", did);
        return success(service.getDemandOptionsBySidAndDid(params));
    }

    /**
     * 根据结构ID获取可用于映射的技术指标选项
     */
    @GetMapping("/indicator_options")
    public AjaxResult getIndicatorOptions(@RequestParam String sid, @RequestParam String iid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        Map<String, Object> params = new HashMap<>();
        params.put("sid", sid);
        params.put("iid", iid);
        return success(service.getIndicatorOptionsBySidAndIid(params));
    }

    /**
     * 根据结构ID获取所有选项型或范围型需求
     */
    @GetMapping("/option_demands/{sid}")
    public AjaxResult getOptionOrRangeDemands(@PathVariable("sid") String sid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return success(service.getOptionOrRangeDemandsBySid(sid));
    }

    /**
     * 根据结构ID获取所有选项型技术指标
     */
    @GetMapping("/option_indicators/{sid}")
    public AjaxResult getOptionIndicators(@PathVariable("sid") String sid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return success(service.getOptionIndicatorsBySid(sid));
    }

    /**
     * 批量添加知识映射
     */
    @Log(title = "知识映射", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult batchAdd(@RequestBody List<DataKnowledgeMapping> mappingList) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return toAjax(service.batchAdd(mappingList));
    }

    /**
     * 根据结构ID删除知识映射
     */
    @Log(title = "知识映射", businessType = BusinessType.DELETE)
    @DeleteMapping("/struct/{sid}")
    public AjaxResult removeByStructId(@PathVariable("sid") String sid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return toAjax(service.removeByStructId(sid));
    }

    /**
     * 根据需求ID删除知识映射
     */
    @Log(title = "知识映射", businessType = BusinessType.DELETE)
    @DeleteMapping("/demand/{did}")
    public AjaxResult removeByDemandId(@PathVariable("did") String did) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return toAjax(service.removeByDemandId(did));
    }

    /**
     * 根据指标ID删除知识映射
     */
    @Log(title = "知识映射", businessType = BusinessType.DELETE)
    @DeleteMapping("/indicator/{iid}")
    public AjaxResult removeByIndicatorId(@PathVariable("iid") String iid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return toAjax(service.removeByIndicatorId(iid));
    }

    /**
     * 根据需求选项ID查找已映射的技术指标
     */
    @GetMapping("/indicator_mappings/{doid}")
    public AjaxResult getIndicatorMappingsByDemandId(@PathVariable("doid") String doid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return success(service.getIndicatorMappingsByDemandId(doid));
    }

    /**
     * 根据技术指标选项ID查找已映射的需求
     */
    @GetMapping("/demand_mappings/{ioid}")
    public AjaxResult getDemandMappingsByIndicatorId(@PathVariable("ioid") String ioid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return success(service.getDemandMappingsByIndicatorId(ioid));
    }

    /**
     * 根据结构ID获取知识映射结果
     */
    @GetMapping("/mapping_results/{sid}")
    public AjaxResult getKnowledgeMappingResultsBySid(@PathVariable("sid") String sid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return success(service.getKnowledgeMappingResultsBySid(sid));
    }

    /**
     * 根据需求选项ID和技术指标选项ID获取映射关系
     */
    @GetMapping("/by_demand_indicator")
    public AjaxResult getMappingsByDemandAndIndicator(
            @RequestParam("doid") String doid,
            @RequestParam("ioid") String ioid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        Map<String, Object> params = new HashMap<>();
        params.put("doid", doid);
        params.put("ioid", ioid);
        return success(service.getMappingsByDemandAndIndicator(params));
    }

    /**
     * 根据结构ID、需求ID和指标ID获取知识映射
     */
    @GetMapping("/getByStructAndDemandAndIndicator")
    public AjaxResult getKnowledgeMappingsByStructAndDemandAndIndicator(
            @RequestParam("sid") String sid,
            @RequestParam("did") String did,
            @RequestParam("iid") String iid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        Map<String, Object> params = new HashMap<>();
        params.put("sid", sid);
        params.put("did", did);
        params.put("iid", iid);
        return success(service.getKnowledgeMappingsByStructAndDemandAndIndicator(params));
    }

    /**
     * 根据需求选项ID获取知识映射结果
     */
    @GetMapping("/getByDoid/{doid}")
    public AjaxResult getKnowledgeMappingResultsByDoid(@PathVariable("doid") String doid) {
        List<DataKnowledgeMappingResult> list = service.getKnowledgeMappingResultsByDoid(doid);
        return AjaxResult.success(list);
    }
} 